/**
 * Created by TAO on 2020/03/06 15:17.
 * 七牛上传相关
 */
import { getUploadToken } from '@/api/user'

const getDefaultState = () => {
  return {
    token: '',
    key: 'service',
    uploadUrl: '//upload.qiniup.com/',
    baseUrl: '//pic.xishuw.com/',
    config: {
      useCdnDomain: true,
      region: {
        srcUphost: 'up.qiniup.com',
        cdnUphost: 'upload.qiniup.com'
      }
    }
  }
}

const state = getDefaultState()

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  }
}

const actions = {
  getToken ({ commit }) {
    getUploadToken().then(res => {
      const { token } = res
      commit('SET_TOKEN', token)
    }).catch(err => {
      console.log(err)
    })
  }
}

const getters = {
  upload (state) {
    return state
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
